/**
 * @method gulpfile 打包工具
 * @author Lkx
 * @create time 2018.05.31
 */

const pkg = require('./package.json');
const gulp = require('gulp');
const browserSync = require('browser-sync').create();
const runSequence = require('run-sequence');
const header = require('gulp-header');
const note = `/**\n * Method Kxui\n * Version ${pkg.version}\n * Author Lkx\n * Create Time ${pkg.createTime}\n * Update Time ${pkg.updateTime}\n * Website http://www.kxui.org || https://lkx2217.gitee.io/kxuiclient/\n * Released under the MIT License\n */\n\n`;
const htmlPath = './test/index.html';
const scssPath = './src/assets/scss/*.scss';
const javascriptPath = ['./src/*.js', './src/modules/*.js', './src/modules.vue/*.js'];

/**
 * @method build 开发构建入口
 * @for gulpfile
 */
gulp.task('build', function () {
  runSequence(['beforeClean'], ['copyFont'], ['revVue'], ['revScss'], ['revCss'], ['revJavascript'], ['rearClean']);
});

/**
 * @method clean 打包前进行文件清理
 * @for build
 */
const del = require('del');
gulp.task('beforeClean', function (cb) {
  return del([
    './dist/**',
    './src/assets/css/**'
  ], cb);
});

/**
 * @method copyFont 复制字体资源文件
 * @for build
 */
gulp.task('copyFont', function () {
  return gulp.src('./src/assets/font/*')
    .pipe(gulp.dest('./dist/assets/font'));
});

/**
 * @method revVue vue代码编译
 * @for build
 */
const rename = require("gulp-rename");
const vueComponent = require('gulp-vue-single-file-component');
gulp.task('revVue', function () {
  return gulp.src(`./src/modules.vue/*.vue`)
    .pipe(vueComponent())
    .pipe(rename(function (path) {
      path.extname = '.js';
    }))
    .pipe(gulp.dest('./src/modules.vue/'));
});

/**
 * @method revScss scss代码编译
 * @for build/watch
 */
let sass = require('gulp-sass');
gulp.task('revScss', function () {
  return gulp.src(scssPath)
    .pipe(sass())
    .pipe(gulp.dest('./src/assets/css'))
    .pipe(browserSync.reload({
      stream: true
    }));
});

/**
 * @method revCss css兼容性处理及压缩打包
 * @for build/watch
 */
let cssMin = require('gulp-csso');
let autoprefixer = require('gulp-autoprefixer');
gulp.task('revCss', function () {
  return gulp.src('./src/assets/css/kxui.css')
    .pipe(autoprefixer())
    .pipe(cssMin())
    .pipe(header(note))
    .pipe(gulp.dest('./dist/assets/css'));
});

/**
 * @method revJavascript javascript代码压缩
 * @for build/watch
 */
let uglify = require('gulp-uglify');
let babel = require("gulp-babel");
gulp.task('revJavascript', function () {
  return gulp.src(javascriptPath, {
      base: './src'
    })
    .pipe(babel())
    .pipe(uglify())
    .pipe(header(note))
    .pipe(gulp.dest('./dist/'))
    .pipe(browserSync.reload({
      stream: true
    }));
});

/**
 * @method clean 打包完成后进行文件清理
 * @for build
 */
gulp.task('rearClean', function (cb) {
  return del([
    './src/assets/css/**',
    './src/modules.vue/*.js'
  ], cb);
});

/**
 * @method dev 本地服务启动入口
 * @for gulpfile
 */
gulp.task('dev', function () {
  runSequence(['watch'], ['server']);
});

/**
 * @method revHtml html代码编译
 * @for watch
 */
gulp.task('revHtml', function () {
  return gulp.src(htmlPath)
    .pipe(gulp.dest('./test'))
    .pipe(browserSync.reload({
      stream: true
    }));
});

/**
 * @method watch 监听开发文件
 * @for dev
 */
gulp.task('watch', ['dev'], function () {
  gulp.watch(htmlPath, ['revHtml']);
  gulp.watch(scssPath, ['revScss']);
  gulp.watch(javascriptPath, ['revJavascript']);
});

/**
 * @method server 启动服务器
 * @for dev
 */
gulp.task('server', function () {
  browserSync.init({
    server: {
      baseDir: './',
      index: htmlPath
    },
    port: 1992
  });
});